package day10;

import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

public class WeatherThread extends Thread{
    private final Socket s;

    public WeatherThread(Socket s) {
        this.s =s;
    }

    @Override
    public void run() {
        System.out.println(s+"进入服务器查询气象信息");
        Random random =new Random();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            InputStream in =s.getInputStream();
            OutputStream out =s.getOutputStream();
            BufferedReader br =new BufferedReader(new InputStreamReader(in));
            PrintWriter pw =new PrintWriter(out,true);
            while(true){
                pw.println("请输入要查询的：1.查询气温 2.风力 3.pm2.5 4.湿度 9.退出");
                String str =br.readLine();
                System.out.println(s+"查询了"+str);
                if (str.equals("1")){
                    Calendar calendar =Calendar.getInstance();
                    String str1 ="当前时间为:"+sdf.format(calendar.getTime())+"当前气温为:"+random.nextInt(30)+"摄氏度";
                    pw.println(str1);
                }else if (str.equals("2")){
                    Calendar calendar =Calendar.getInstance();
                    String str2 ="当前时间为:"+sdf.format(calendar.getTime())+"当前风力为:"+random.nextInt(10)+"级";
                    pw.println(str2);
                }else if (str.equals("3")){
                    Calendar calendar =Calendar.getInstance();
                    String str3 ="当前时间为:"+sdf.format(calendar.getTime())+"pm2.5为:"+random.nextInt(20)+"纳米";
                    pw.println(str3);
                }else if (str.equals("4")){
                    Calendar calendar =Calendar.getInstance();
                    String str4 ="当前时间为:"+sdf.format(calendar.getTime())+"湿度为:"+random.nextInt(100)+"立方";
                    pw.println(str4);
                }else if (str.equals("9")){
                    pw.println("退出成功！欢迎下次登录!");
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                s.close();
                System.out.println(s+"结束查询,查询结束了!");
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
}